package com.hudilao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hudilao.dto.DishSaleDTO;
import com.hudilao.entity.Order;
import com.hudilao.entity.OrderDetail;
import com.hudilao.mapper.OrderDetailMapper;
import com.hudilao.mapper.OrderMapper;
import com.hudilao.service.OrderDetailService;
import com.hudilao.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 根据订单id查询订单详情
     * @param orderId
     * @return
     */
    public List<OrderDetail> getOrderDetailsByOrderId(long orderId) {
        return orderDetailMapper.getOrderDetailsByOrderId(orderId);
    }

    /**
     * 根据顾客id查询其点过的订单详情
     * @param customerId
     * @return
     */
    public List<OrderDetail> getOrderDetailsByCustomerId(long customerId) {

        //获取顾客点过的订单
        List<Order> orders = orderMapper.getOrdersById(customerId);

        // 初始化一个列表来存储所有的订单详情
        List<OrderDetail> allOrderDetails = new ArrayList<>();

        // 遍历每个订单，获取每个订单的订单详情
        for (Order order : orders) {
            // 根据订单id查询订单详情
            List<OrderDetail> orderDetails = orderDetailMapper.getOrderDetailsByOrderId(order.getId());
            allOrderDetails.addAll(orderDetails);
        }

        return allOrderDetails;
    }


    //获取销量前五菜品
    @Override
    public List<DishSaleDTO> getTopSellingDishes(String timeRange) {

        //筛选时间
        LocalDateTime startTime;
        if ("weekly".equals(timeRange)) {
            //本周
            startTime = LocalDateTime.now().minusWeeks(1);
        } else if ("monthly".equals(timeRange)) {
            //本月
            startTime = LocalDateTime.now().minusMonths(1);
        } else {
            //本日
            startTime = LocalDateTime.now().minusDays(1);
        }

        // 查询单独菜品和套餐中的菜品销售数据
        List<DishSaleDTO> dishSales = orderDetailMapper.selectDishSales(startTime);

        List<DishSaleDTO> setmealDishSales = orderDetailMapper.selectSetmealDishSales(startTime);

        // 合并结果
        //存放合并后的菜品销售数量的数据
        Map<Long, DishSaleDTO> salesMap = new HashMap<>();
        //处理单独菜品的销量
        dishSales.forEach(dish -> salesMap.put(dish.getDishId(), dish));

        for (DishSaleDTO setmealDish : setmealDishSales) {
            DishSaleDTO existing = salesMap.get(setmealDish.getDishId());
            if (existing != null) {
                existing.setQuantity(existing.getQuantity() + setmealDish.getQuantity());
            } else {
                salesMap.put(setmealDish.getDishId(), setmealDish);
            }
        }

        // 按销量排序并取前五
        return salesMap.values().stream()
                .sorted(Comparator.comparing(DishSaleDTO::getQuantity).reversed())
                .limit(5)
                .collect(Collectors.toList());
    }
}
